﻿$(document).ready(main);

function intializeInbox() {
    //initializeCreditRequestAsignation();
    initializeAprovalFormRequest();
}

//function initializeCreditRequestAsignation() {
//    //Le mando el groupchiefid
//    loadModalAsignCreditRequest(7);
//}

function loadModalAsignCreditRequest(idGroupChief,idRequest) {
    $.ajax({
        url: "\CreditRequestAsignationModal",
        type: "post",
        data: { idRequest: parseInt(idRequest), idGroupChief: parseInt(idGroupChief) },
        dataType: "json",
        success: function (resp) {
            $(".content-inbox-modal-view").html(resp.view);
            $('.modal-header').children('h2').remove();
            $('.modal-header').append($('<h2>').text(resp.title));
        }
    });
}

function initializeAprovalFormRequest() {


    var idAnswer = $("#modal_approval_idapp").attr("value");
    $(".link-modal-approval-form-request").bind("click", function () {
        var idRequest = $(this).text();
        $.ajax({
            url: "\ApprovalFormModal",
            type: "post",
            data: { idAnswer: parseInt(idAnswer) },
            dataType: "json",
            success: function (resp) {
                $(".content-inbox-modal-view").html(resp.view);
            }
        });



    });
}

function main() {
    updateMailbox2();
    updateMailbox4();
    setInterval(function () {
        //alert("1/2 min");
        //updateMailbox1();
        updateMailbox2();
        //updateMailbox3();
        updateMailbox4();
        intializeInbox();
    }, 10000);
}

function updateMailbox1() {

}

function updateMailbox2() {
    var escritor = "";
    $("#monita tr").remove();
    //$("#div_assign_1 table")
    $.ajax({
        url: "\UpdateMailbox2",
        type: "post",
        data: {},
        dataType: "json",
        success: function (data) {
            //$("#lblMessage").text(data.result);
            //console.log(data);
            //escritor += "<table class='table-bordered table-striped table-condensed flip-content' id='assign_1'>" +
            //                "<thead class='flip-content'>" +
            //                    "<tr>" +
            //                        "<th>#</th>" +
            //                        "<th>NUMERO SOLICITUD</th>" +
            //                        "<th>EMPRESA</th>" +
            //                        "<th>GRUPO</th>" +
            //                        "<th class='hidden-480'>FECHA REGISTRO</th>" +
            //                    "</tr>" +
            //                "</thead>" +
            //                "<tbody>";
            $.each(data, function (i, item) {
                var tr = $('<tr>');
                var rowid = $('<td>').text(parseInt(i + 1));
                var modalLink = $('<a>').attr('class', 'link-modal-asign-credit-request').attr('data-toggle', 'modal').attr('href', '#responsive').text(item.requestId);
                $(modalLink).bind('click', function () {
                    //Mandar groupchiefid
                    loadModalAsignCreditRequest(2,$(this).text());
                });
                var requestId = $('<td>').append(modalLink);
                var socialReason = $('<td>').text(item.socialReason);
                var group = $('<td>').text(item.group);
                var recordDate = $('<td>').attr('class', 'hidden-480').text(item.recordDate);
                tr.append(rowid, requestId, socialReason, group, recordDate);
                //escritor += "<tr>" +
                //                '<td>' + parseInt(i + 1) + '</td>' +
                //                '<td><a class="link-modal-asign-credit-request" data-toggle="modal" href="#responsive">' + item.requestId + '</a></td>' +
                //                '<td>' + item.socialReason + '</td>' +
                //                '<td>' + item.group + '</td>' +
                //                '<td class="hidden-480">' + item.recordDate + '</td>' +
                //            "</tr>";
                $("#monita").append(tr);
            });
        }

    });
}

function updateMailbox3() {


}

function updateMailbox4() {
    //$.ajax({
    //    url: "\UpdateMailbox4",
    //    type: "post",
    //    data: {},
    //    dataType: "json",
    //    success: function (data) {
    //        $.each(data.response, function (i, item) {
    //            var tr = $('<tr>');
    //            var rowid = $('<td>').text(parseInt(i + 1)); //rowid
    //            var modalLink = $('<a>').attr('class', 'class-class').attr('data-toggle', 'modal').attr('href', '#responsive').text(item.idotorgamiento);
    //            var requestId = $('<td>').append(modalLink);
    //            var ammountId = $('<td>').text(item.monto);
    //            var socialReason = $('<td>').text(item.empresa);
             
    //            var recordDate = $('<td>').attr('class', 'hidden-480').text(item.fecha);
    //            tr.append(rowid, requestId,ammountId, socialReason, group, recordDate); 
    //            $("#inbox4_table").append(tr);
    //        });
    //    }
    //});
}